#include "usart.h"
#include "app_crc.h"
#include "app_timeout.h"
#include "string.h"
#include "assert.h"
#include "app_mp.h"


#define USART3_EN_GPIO		GPIOB
#define USART3_EN_PIN		GPIO_PIN_12

#define USART3_EN_SET		gpio_bit_set(USART3_EN_GPIO, USART3_EN_PIN)
#define USART3_EN_RESET		gpio_bit_reset(USART3_EN_GPIO, USART3_EN_PIN)


uint8_t usart_rx_buff[1024];

uint16_t usart_p=0;

uint8_t rec_data[1024];

uint16_t txbuff[10];

uint16_t *length=0;

uint8_t sendch;

uint16_t crcrec;

uint16_t crcplay;

uint16_t crctest;
uint16_t crccpy;

//uint8_t test[6]={0x02,0x10,0x00,0xCB,0x00,0x01};

//uint16_t set_v_para=set_v/10;
//uint16_t set_a_para=set_a/100;

uint16_t set_v_para=set_v;
uint16_t set_a_para=set_a;

char *get_rebuff(uint16_t *len);

void clean_rebuff(void);

//bool step_on_1(void);

void read_set_info(int readnum);

void read_real_info(int readnum);

bool reg200(int setnum);

bool reg201(int setnum);

bool reg202(int setnum);

bool reg203(int setnum);

bool reg204(int setnum);

void gd_eval_com_init1(void);


//typedef struct
//{
//	uint8_t  address;
//	uint8_t  function;
//	uint16_t regstart;
//	uint16_t number;
//	uint16_t crc;
//}usart_tx_struct;
//usart_tx_struct tdata;
uint8_t buff[8]={0};

uint8_t setbuff[19]={0};

uint16_t swapBytes(uint16_t value) {
    return ((value & 0xFF) << 8) | ((value >> 8) & 0xFF);
}


//bool app_usart_task_poll(uint8_t *rx_data)
//{
//	get_rebuff(length);
//	if(*length==0)
//	{
//		return FALSE;
//		
//	}
//	else
//	{
//		if(*length==1024)
//		{
//			rx_data[*length]=usart_rx_buff[*length];
//			clean_rebuff();
//			usart_p=0;
//			return TRUE;
//		}
//		else
//		{
//			rx_data[*length]=usart_rx_buff[*length];
//			
//			return TRUE;
//		}
//	}
//}

app_timeout_struct waitmessage0;
void read_real_info(int readnum)
{	
		clean_rebuff();
		usart_p=0;
	
		buff[0]=0x00;
		buff[1]=0x04;
		buff[2]=0x01;
		buff[3]=0x90;
		buff[4]=0x00;
		buff[5]=readnum;
		buff[6]=(crc16(buff, 6)&(0xFF00))>>8;
		buff[7]=crc16(buff,6)&(0x00FF);
		USART3_EN_SET;
		for(int i=0;i<8;i++)
		{
			usart_data_transmit(USART1, buff[i]);
			while (RESET == usart_flag_get(USART1, USART_FLAG_TC));
		}
		
//		tdata.address=j;
//		tdata.function=0x04;
//		tdata.regstart=0x0190;
//		tdata.number=0x000A;
//		tdata.crc=0x71EF;
		USART3_EN_RESET;
		while(1)
		{
			if(app_timeout_judge( &waitmessage0, 500))
			break;
		}
//		if(app_timeout_judge( &waitmessage0, 500))
//		{
//			get_rebuff(length);
			uint16_t recive[usart_p-5];
			memcpy((uint16_t *)&crcrec,&usart_rx_buff[usart_p-2],2);
			crcplay=crc16(usart_rx_buff,usart_p-2);
			crccpy=swapBytes(crcrec);
//			uint8_t fun=usart_rx_buff[1];
			if(crccpy!=crcplay)
			{
				return ;
			}
			else
			{
				uint8_t bytes=usart_rx_buff[2];
				for(int i=0;i<bytes;i++)
				{
					
					recive[i]=(((usart_rx_buff[i+3])&(0xFFFF))<<8)&((usart_rx_buff[i+4])&(0xFFFF));
					memcpy(&txbuff[i],&recive[i],2);
				}
				app_mp_send_data(mp_cmd_81,(uint8_t *)&txbuff[0],8);
				app_mp_send_data(mp_cmd_81,(uint8_t *)&txbuff[4],8);
				app_mp_send_data(mp_cmd_81,(uint8_t *)&txbuff[8],4);
			}
			
}
app_timeout_struct waitmessage1;
void read_set_info(int readnum)
{	
		
		clean_rebuff();
		usart_p=0;
	
		buff[0]=0x00;
		buff[1]=0x04;
		buff[2]=0x00;
		buff[3]=0xc8;
		buff[4]=0x00;
		buff[5]=readnum;
		buff[6]=(crc16(buff, 6)&(0xFF00))>>8;
		buff[7]=crc16(buff,6)&(0x00FF);
		USART3_EN_SET;
		for(int i=0;i<8;i++)
		{
			usart_data_transmit(USART1, buff[i]);
			while (RESET == usart_flag_get(USART1, USART_FLAG_TC));
		}
		
//		tdata.address=j;
//		tdata.function=0x04;
//		tdata.regstart=0x0190;
//		tdata.number=0x000A;
//		tdata.crc=0x71EF;
		USART3_EN_RESET;
		while(1)
		{
			if(app_timeout_judge( &waitmessage1, 500))
			break;
		}
//		if(app_timeout_judge( &waitmessage1, 500))
//		{
//			get_rebuff(length);
			uint16_t recive[usart_p-5];
			memcpy((uint16_t *)&crcrec,&usart_rx_buff[usart_p-2],2);
			crcplay=crc16(usart_rx_buff,usart_p-2);
			crccpy=swapBytes(crcrec);
			if(crcplay==crccpy)
			{
				uint8_t bytes=usart_rx_buff[2];
				for(int i=0;i<bytes;i++)
				{
					recive[i]=((usart_rx_buff[i+3])&(0xFFFF)<<8)&((usart_rx_buff[i+4])&(0xFFFF));
					memcpy(&txbuff[i],&recive[i],2);
				}
				app_mp_send_data(mp_cmd_81,(uint8_t *)txbuff,8);
				
			}
			else
			{
				return ;
			}
	
}
app_timeout_struct waitmessage2;
bool reg200(int setnum)
{	
	assert(setnum > 0);
	uint8_t firstbool=0;
	uint8_t secondbool=0;
	
	if(setnum<4)
	{
		firstbool=0;
	}
	else if(setnum==4)
	{
		firstbool=1;
		secondbool=0;
	}
	else
	{
		firstbool=1;
		secondbool=1;
	}
	
		clean_rebuff();
		usart_p=0;
	
		setbuff[0]=0x00;
		setbuff[1]=0x10;
		setbuff[2]=0x00;
		setbuff[3]=0xc8;
		setbuff[4]=0x00;
		setbuff[5]=0x05;
		setbuff[6]=0x0A;
		setbuff[7]=((set_v_para)&(0xFF00))>>8;
		setbuff[8]=(set_v_para)&(0x00FF);
//		setbuff[9]=0x00;
//		setbuff[10]=0x00;
//		setbuff[11]=0x00;
//		setbuff[12]=0x00;
	
	if(firstbool==1)
	{
		setbuff[13]=((set_a_para)&(0xFF00))>>8;
		setbuff[14]=(set_a_para)&(0x00FF);
	}
	
	if(secondbool==1)
	{
		setbuff[15]=0x00;
		setbuff[16]=0x01;
	}
	
		setbuff[17]=(crc16(setbuff, 17)&(0xFF00))>>8;
		setbuff[18]=crc16(setbuff,17)&(0x00FF);
//		buff[0]=j;
//		buff[1]=0x04;
//		buff[2]=0x01;
//		buff[3]=0x90;
//		buff[4]=0x00;
//		buff[5]=0x0A;
//		buff[6]=(app_util_crc16(buff, 6)&(0xFF00))>>8;
//		buff[7]=app_util_crc16(buff,6)&(0x00FF);
		USART3_EN_SET;
		usart_interrupt_flag_clear(USART1, USART_INT_FLAG_TC);
		for(int i=0;i<19;i++)
		{
			usart_data_transmit(USART1, setbuff[i]);
			while (RESET == usart_flag_get(USART1, USART_FLAG_TC));
		}
		
//		tdata.address=j;
//		tdata.function=0x04;
//		tdata.regstart=0x0190;
//		tdata.number=0x000A;
//		tdata.crc=0x71EF;
		USART3_EN_RESET;
		while(1)
		{
			if(app_timeout_judge( &waitmessage2, 500))
			break;
		}
		
//		if(app_timeout_judge( &waitmessage2, 500))
//		{
			
//			get_rebuff(length);
//			crctest=crc16(test,6);
			crcplay=crc16(usart_rx_buff,usart_p-2);
			memcpy((uint16_t *)&crcrec,&usart_rx_buff[usart_p-2],2);
			crccpy=swapBytes(crcrec);
			if(crccpy!=crcplay)
			{
				return FALSE;
			}
			else
			{
				return TRUE;
			}
			
//			else
//			{
//				uint8_t bytes=usart_rx_buff[2];
//				for(int i=0;i<bytes;i++)
//				{
//					uint16_t recive[*length-5];
//					recive[i]=((usart_rx_buff[i+3])&(0xFFFF)<<8)&((usart_rx_buff[i+4])&(0xFFFF));
//				}
//			}
//			
//		}
//			memset(usart_rx_buff,0,1024);
}
app_timeout_struct waitmessage3;
bool reg201(int setnum)
{
	assert(setnum > 0);
	uint8_t firstbool=0;
	uint8_t secondbool=0;
	
	if(setnum<3)
	{
		firstbool=0;
	}
	else if(setnum==3)
	{
		firstbool=1;
		secondbool=0;
	}
	else
	{
		firstbool=1;
		secondbool=1;
	}
	
		clean_rebuff();
		usart_p=0;
	
		setbuff[0]=0x00;
		setbuff[1]=0x10;
		setbuff[2]=0x00;
		setbuff[3]=0xc8;
		setbuff[4]=0x00;
		setbuff[5]=0x05;
		setbuff[6]=0x0A;
//		setbuff[7]=(set_v_para)&(0xFF00)>>8;
//		setbuff[8]=(set_v_para)&(0x00FF);
//		setbuff[9]=0x00;
//		setbuff[10]=0x00;
//		setbuff[11]=0x00;
//		setbuff[12]=0x00;
	
	if(firstbool==1)
	{
		setbuff[13]=((set_a_para)&(0xFF00))>>8;
		setbuff[14]=(set_a_para)&(0x00FF);
	}
	
	if(secondbool==1)
	{
		setbuff[15]=0x00;
		setbuff[16]=0x01;
	}
	
		setbuff[17]=(crc16(setbuff, 17)&(0xFF00))>>8;
		setbuff[18]=crc16(setbuff,17)&(0x00FF);
//		buff[0]=j;
//		buff[1]=0x04;
//		buff[2]=0x01;
//		buff[3]=0x90;
//		buff[4]=0x00;
//		buff[5]=0x0A;
//		buff[6]=(app_util_crc16(buff, 6)&(0xFF00))>>8;
//		buff[7]=app_util_crc16(buff,6)&(0x00FF);
		for(int i=0;i<19;i++)
		{
			usart_data_transmit(UART3, setbuff[i]);
			while (RESET == usart_flag_get(UART3, USART_FLAG_TC));
		}
		
//		tdata.address=j;
//		tdata.function=0x04;
//		tdata.regstart=0x0190;
//		tdata.number=0x000A;
//		tdata.crc=0x71EF;
		while(1)
		{
			if(app_timeout_judge( &waitmessage3, 500))
			break;
		}
//		if(app_timeout_judge( &waitmessage3, 500))
//		{
			
//			get_rebuff(length);
			crcplay=crc16(usart_rx_buff,usart_p-2);
			memcpy((uint16_t *)&crcrec,&usart_rx_buff[usart_p-2],2);
			crccpy=swapBytes(crcrec);
			if(crccpy!=crcplay)
			{
				return FALSE;
			}
			else
			{
				return TRUE;
			}
			
//			else
//			{
//				uint8_t bytes=usart_rx_buff[2];
//				for(int i=0;i<bytes;i++)
//				{
//					uint16_t recive[*length-5];
//					recive[i]=((usart_rx_buff[i+3])&(0xFFFF)<<8)&((usart_rx_buff[i+4])&(0xFFFF));
//				}
//			}
//			
//		}
	
}
app_timeout_struct waitmessage4;
bool reg202(int setnum)
{	
	assert(setnum > 0);
	uint8_t firstbool=0;
	uint8_t secondbool=0;
	
	if(setnum<2)
	{
		firstbool=0;
	}
	else if(setnum==2)
	{
		firstbool=1;
		secondbool=0;
	}
	else
	{
		firstbool=1;
		secondbool=1;
	}
	
		clean_rebuff();
		usart_p=0;
	
		setbuff[0]=0x00;
		setbuff[1]=0x10;
		setbuff[2]=0x00;
		setbuff[3]=0xc8;
		setbuff[4]=0x00;
		setbuff[5]=0x05;
		setbuff[6]=0x0A;
//		setbuff[7]=(set_v_para)&(0xFF00)>>8;
//		setbuff[8]=(set_v_para)&(0x00FF);
//		setbuff[9]=0x00;
//		setbuff[10]=0x00;
//		setbuff[11]=0x00;
//		setbuff[12]=0x00;
	
	if(firstbool==1)
	{
		setbuff[13]=((set_a_para)&(0xFF00))>>8;
		setbuff[14]=(set_a_para)&(0x00FF);
	}
	
	if(secondbool==1)
	{
		setbuff[15]=0x00;
		setbuff[16]=0x01;
	}
	
		setbuff[17]=(crc16(setbuff, 17)&(0xFF00))>>8;
		setbuff[18]=crc16(setbuff,17)&(0x00FF);
//		buff[0]=j;
//		buff[1]=0x04;
//		buff[2]=0x01;
//		buff[3]=0x90;
//		buff[4]=0x00;
//		buff[5]=0x0A;
//		buff[6]=(app_util_crc16(buff, 6)&(0xFF00))>>8;
//		buff[7]=app_util_crc16(buff,6)&(0x00FF);
		for(int i=0;i<19;i++)
		{
			usart_data_transmit(UART3, setbuff[i]);
			while (RESET == usart_flag_get(UART3, USART_FLAG_TC));
		}
		
//		tdata.address=j;
//		tdata.function=0x04;
//		tdata.regstart=0x0190;
//		tdata.number=0x000A;
//		tdata.crc=0x71EF;
		while(1)
		{
			if(app_timeout_judge( &waitmessage4, 500))
			break;
		}
//		if(app_timeout_judge( &waitmessage4, 500))
//		{
			
			crcplay=crc16(usart_rx_buff,usart_p-2);
			memcpy((uint16_t *)&crcrec,&usart_rx_buff[usart_p-2],2);
			crccpy=swapBytes(crcrec);
			if(crccpy!=crcplay)
			{
				return FALSE;
			}
			else
			{
				return TRUE;
			}
//			else
//			{
//				uint8_t bytes=usart_rx_buff[2];
//				for(int i=0;i<bytes;i++)
//				{
//					uint16_t recive[*length-5];
//					recive[i]=((usart_rx_buff[i+3])&(0xFFFF)<<8)&((usart_rx_buff[i+4])&(0xFFFF));
//				}
//			}
//			
//		}

}
app_timeout_struct waitmessage5;
bool reg203(int setnum)
{
	assert(setnum > 0);
	uint8_t firstbool=0;
	uint8_t secondbool=0;
	if(setnum<1)
	{
		firstbool=0;
	}
	else if(setnum==1)
	{
		firstbool=1;
		secondbool=0;
	}
	else
	{
		firstbool=1;
		secondbool=1;
	}
	
		clean_rebuff();
		usart_p=0;
	
		setbuff[0]=0x00;
		setbuff[1]=0x10;
		setbuff[2]=0x00;
		setbuff[3]=0xc8;
		setbuff[4]=0x00;
		setbuff[5]=0x05;
		setbuff[6]=0x0A;
//		setbuff[7]=(set_v_para)&(0xFF00)>>8;
//		setbuff[8]=(set_v_para)&(0x00FF);
//		setbuff[9]=0x00;
//		setbuff[10]=0x00;
//		setbuff[11]=0x00;
//		setbuff[12]=0x00;
	
	if(firstbool==1)
	{
		setbuff[13]=((set_a_para)&(0xFF00))>>8;
		setbuff[14]=(set_a_para)&(0x00FF);
	}
	
	if(secondbool==1)
	{
		setbuff[15]=0x00;
		setbuff[16]=0x01;
	}
	
		setbuff[17]=(crc16(setbuff, 17)&(0xFF00))>>8;
		setbuff[18]=crc16(setbuff,17)&(0x00FF);
//		buff[0]=j;
//		buff[1]=0x04;
//		buff[2]=0x01;
//		buff[3]=0x90;
//		buff[4]=0x00;
//		buff[5]=0x0A;
//		buff[6]=(app_util_crc16(buff, 6)&(0xFF00))>>8;
//		buff[7]=app_util_crc16(buff,6)&(0x00FF);
		for(int i=0;i<19;i++)
		{
			usart_data_transmit(UART3, setbuff[i]);
			while (RESET == usart_flag_get(UART3, USART_FLAG_TC));
		}
		
//		tdata.address=j;
//		tdata.function=0x04;
//		tdata.regstart=0x0190;
//		tdata.number=0x000A;
//		tdata.crc=0x71EF;
		while(1)
		{
			if(app_timeout_judge( &waitmessage5, 500))
			break;
		}
//		if(app_timeout_judge( &waitmessage5, 500))
//		{
			
			crcplay=crc16(usart_rx_buff,usart_p-2);
			memcpy((uint16_t *)&crcrec,&usart_rx_buff[usart_p-2],2);
			crccpy=swapBytes(crcrec);
			if(crccpy!=crcplay)
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
//			else
//			{
//				uint8_t bytes=usart_rx_buff[2];
//				for(int i=0;i<bytes;i++)
//				{
//					uint16_t recive[*length-5];
//					recive[i]=((usart_rx_buff[i+3])&(0xFFFF)<<8)&((usart_rx_buff[i+4])&(0xFFFF));
//				}
//			}
//			
//		}
}

app_timeout_struct waitmessage6;
bool reg204(int setnum)
{
	assert(setnum > 0);
	uint8_t firstbool=0;
	uint8_t secondbool=0;
//	memset(&setbuff,0,19);
	if(setnum>=1)
	{
		secondbool=1;
	}
		
		clean_rebuff();
		usart_p=0;
	
		setbuff[0]=0x00;
		setbuff[1]=0x10;
		setbuff[2]=0x00;
		setbuff[3]=0xc8;
		setbuff[4]=0x00;
		setbuff[5]=0x05;
		setbuff[6]=0x0a;
//		setbuff[7]=(set_v_para)&(0xFF00)>>8;
//		setbuff[8]=(set_v_para)&(0x00FF);
//		setbuff[9]=0x00;
//		setbuff[10]=0x00;
//		setbuff[11]=0x00;
//		setbuff[12]=0x00;
	
	if(firstbool==1)
	{
		setbuff[13]=((set_a_para)&(0xFF00))>>8;
		setbuff[14]=(set_a_para)&(0x00FF);
	}
	
	if(secondbool==1)
	{
		setbuff[15]=0x00;
		setbuff[16]=0x01;
	}
	
		setbuff[17]=(crc16(setbuff, 17)&(0xFF00))>>8;
		setbuff[18]=crc16(setbuff,17)&(0x00FF);
//		buff[0]=j;
//		buff[1]=0x04;
//		buff[2]=0x01;
//		buff[3]=0x90;
//		buff[4]=0x00;
//		buff[5]=0x0A;
//		buff[6]=(app_util_crc16(buff, 6)&(0xFF00))>>8;
//		buff[7]=app_util_crc16(buff,6)&(0x00FF);
		USART3_EN_SET;
		for(int i=0;i<19;i++)
		{
			usart_data_transmit(USART1, setbuff[i]);
			while (RESET == usart_flag_get(USART1, USART_FLAG_TC));
		}
		
//		tdata.address=j;
//		tdata.function=0x04;
//		tdata.regstart=0x0190;
//		tdata.number=0x000A;
//		tdata.crc=0x71EF;
		USART3_EN_RESET;
		while(1)
		{
			if(app_timeout_judge( &waitmessage6, 500))
			break;
		}
//		if(app_timeout_judge( &waitmessage6, 500))
//		{
			
			crcplay=crc16(usart_rx_buff,usart_p-2);
			memcpy((uint16_t *)&crcrec,&usart_rx_buff[usart_p-2],2);
			crccpy=swapBytes(crcrec);
			if(crccpy!=crcplay)
			{
				return FALSE;
			}
			else
			{
				return TRUE;
			}
//			else
//			{
//				uint8_t bytes=usart_rx_buff[2];
//				for(int i=0;i<bytes;i++)
//				{
//					uint16_t recive[*length-5];
//					recive[i]=((usart_rx_buff[i+3])&(0xFFFF)<<8)&((usart_rx_buff[i+4])&(0xFFFF));
//				}
//			}
//			
//		}

}

void  UART3_IRQHandler(void)
{
	if(usart_p<USART_BUFF_SIZE)
	{
		if(usart_flag_get(UART3,USART_FLAG_RBNE)!=RESET)
		{
			usart_rx_buff[usart_p]=usart_data_receive(UART3);
			usart_p++;
			
			usart_interrupt_flag_clear(UART3,USART_INT_FLAG_RBNE);
		}
	}
	else
	{
		usart_interrupt_flag_clear(UART3,USART_INT_FLAG_RBNE);
	}
//	if(usart_interrupt_flag_get(UART3, USART_INT_FLAG_TC) != RESET)
//	{
//		usart_interrupt_flag_clear(UART3, USART_INT_FLAG_TC);
//	}
}

char *get_rebuff(uint16_t *len)
{
	*len=usart_p;
	return (char *)&usart_rx_buff;
}

void clean_rebuff(void)
{
	uint16_t i=USART_BUFF_SIZE+1;
	usart_p=0;
	while(i)
	{
		usart_rx_buff[--i]=0;
	}
}
void gd_eval_com_init1(void)
{
//    uint32_t com_id = 0U;
//    if(EVAL_COM0 == com){
//        com_id = 0U;
//    }else if(EVAL_COM1 == com){
//        com_id = 1U;
//    }
//    
    /* enable GPIO clock */
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_GPIOC);

    /* enable USART clock */
    rcu_periph_clock_enable(RCU_USART1);
	
	gpio_init(USART3_EN_GPIO, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, USART3_EN_PIN);

    /* connect port to USARTx_Tx */
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);

    /* connect port to USARTx_Rx */
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_3);

    /* USART configure */
    usart_deinit(USART1);
    usart_baudrate_set(USART1, 9600U);
    usart_word_length_set(USART1, USART_WL_8BIT);
    usart_stop_bit_set(USART1, USART_STB_1BIT);
    usart_parity_config(USART1, USART_PM_NONE);
    usart_hardware_flow_rts_config(USART1, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(USART1, USART_CTS_DISABLE);
    usart_receive_config(USART1, USART_RECEIVE_ENABLE);
    usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
    usart_enable(USART1);
	nvic_irq_enable(USART1_IRQn, 0, 0);
	usart_interrupt_enable(USART1,USART_INT_RBNE);
	
	USART3_EN_RESET;
}

//app_timeout_struct lost;
//if(app_timeout_judge(&lost,60000)==)
